using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MyFirstGame.Objects
{
    class SpaceCraft
    {
        private Vector2 _position;
        private Texture2D _texture;
        private int _life;
        private GraphicsDeviceManager _graphics;

        public SpaceCraft(GraphicsDeviceManager graphics)
        {
            this._graphics = graphics;
            this._life = 100;
            this._position = new Vector2(0, 0);
        }

        public Vector2 Position
        {
            get
            {
                return (this._position);
            }
            set
            {
                this._position = value;
            }
        }

        public Vector2 CenterPosition
        {
            get
            {
                return (new Vector2(this._position.X + (this._texture.Width / 2), this._position.Y + (this._texture.Height / 2)));
            }
        }

        public Texture2D Texture
        {
            get
            {
                return (this._texture);
            }
            set
            {
                this._texture = value;
            }
        }

        public int Life
        {
            get
            {
                return (this._life);
            }
        }

        public void DecreaseLife(int amount)
        {
            this._life = this._life - amount;

            if (this._life < 0)
            {
                this._life = 0;
            }
        }

        public void IncreaseLife(int amount)
        {
            this._life = this._life + amount;

            if (this._life > 100)
            {
                this._life = 100;
            }
        }

        public void UpdatePosition(float offsetX, float offsetY)
        {
            this._position.X += offsetX;
            this._position.Y += offsetY;
            this.CheckSpritePosition(ref this._position, this._texture);
        }

        #region Checks and Validates
        private void CheckSpritePosition(ref Vector2 spritePosition, Texture2D spriteTexture)
        {
            int maxX = _graphics.GraphicsDevice.Viewport.Width - spriteTexture.Width;
            int minX = 0;
            int maxY = _graphics.GraphicsDevice.Viewport.Height - spriteTexture.Height;
            int minY = 0;

            // Check for bounce.
            if (spritePosition.X > maxX)
            {
                spritePosition.X = maxX;
            }

            else if (spritePosition.X < minX)
            {
                spritePosition.X = minX;
            }

            if (spritePosition.Y > maxY)
            {
                spritePosition.Y = maxY;
            }

            else if (spritePosition.Y < minY)
            {
                spritePosition.Y = minY;
            }
        }
        #endregion
    }
}
